package com.eqxiu.ieditor.weixin.service;

import com.eqxiu.ieditor.weixin.api.WeChatKeywordServiceApi;
import com.eqxiu.ieditor.weixin.dao.WeixinReplyDao;
import com.eqxiu.ieditor.weixin.dto.KeywordDto;
import com.eqxiu.ieditor.weixin.model.WeixinReply;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeChatKeywordServiceApiImpl implements WeChatKeywordServiceApi {
    private static final Logger LOGGER = LoggerFactory.getLogger(WeChatKeywordServiceApiImpl.class);
    @Autowired
    private WeixinReplyDao weixinReplyDao;

    @Override
    public Map<String, Object> findByParam(KeywordDto dto) {
        if (StringUtils.isNotBlank(dto.getReplyContent())){
            dto.setReplyContent("%"+dto.getReplyContent()+"%");
        }
        List<KeywordDto> byParam = weixinReplyDao.findByParam(dto);
        Integer count = weixinReplyDao.countByParam(dto);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("rows", byParam);
        return map;
    }

    @Override
    public Integer checkMatchContent(String matchContent) {
        Integer count = weixinReplyDao.findByMatchContent(matchContent);
        return count;
    }
    @Override
    public Integer checkMatchContentExcludeId(String matchContent, Integer id) {
        Integer count = weixinReplyDao.checkMatchContentExcludeId(matchContent,id);
        return count;
    }
    @Override
    public Integer save(KeywordDto keywordDto) {
        WeixinReply reply=new WeixinReply();
        reply.setRuleName(keywordDto.getMatchContent());
        reply.setMatchContent(keywordDto.getMatchContent());
        reply.setReplyContent(keywordDto.getReplyContent());
        reply.setMatchMode("contain");
        reply.setType("text");
        reply.setReplyType("text");
        Integer count = weixinReplyDao.save(reply);
        return count;
    }

    @Override
    public Integer update(KeywordDto keywordDto) {
        Integer count = weixinReplyDao.update(keywordDto);
        return count;
    }

    @Override
    public Integer delete(Integer id) {
       return weixinReplyDao.deleteByPrimaryKey(id);
    }
}
